package com.jinshan.gaietymall.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinshan.gaietymall.core.exception.BusinessException;
import com.jinshan.gaietymall.entity.TUser;
import com.jinshan.gaietymall.entity.request.TUserReqVO;
import com.jinshan.gaietymall.entity.request.TuerReqInsertVO;
import com.jinshan.gaietymall.entity.request.TuserQueryVO;
import com.jinshan.gaietymall.entity.response.TUserRep;
import com.jinshan.gaietymall.exception.ErrorCodesEnum;
import com.jinshan.gaietymall.service.ITUserService;
import com.jinshan.gaietymall.utils.BasePageResponseResult;
import com.jinshan.gaietymall.utils.BaseResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-04-15
 */
@RestController
@RequestMapping("/admin/user")
@CrossOrigin
public class TUserController {

    @Autowired
    private ITUserService itUserService;


    /**
      * @description  用户列表
      * @author liujinshan
      * @date 2020/4/24 13:01
      */
    @PostMapping(value = "/getUserPageInfo" , produces = {"application/json"}, consumes = {"application/json"})
    public BasePageResponseResult<TUserRep> getUserPageInfo(@RequestBody TuserQueryVO tuserQueryVO){
        BasePageResponseResult<TUserRep> basePageResponseResult = new BasePageResponseResult<>();
        try {
            IPage<TUserRep> userPage = itUserService.getAll(tuserQueryVO);
            basePageResponseResult = BasePageResponseResult.ok(userPage);
        } catch (BusinessException e) {
            basePageResponseResult.setErrMsg(e.getMsg());
            basePageResponseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            basePageResponseResult.setErrMsg(e.toString());
            basePageResponseResult.setErrCode("测试");
        }
       return basePageResponseResult;
    }

    /**
      * @description 根据ID查询用户信息
      * @author liujinshan
      * param null
      * @return 
      * @throws 
      * @date 2020/4/24 13:02
      */
    @PostMapping(value = "/getById" , produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TUserRep> getById(@RequestBody TuserQueryVO tuserQueryVO){
        BaseResponseResult<TUserRep> responseResult = new BaseResponseResult<>();
        try {
            responseResult = BaseResponseResult.ok(itUserService.getById(tuserQueryVO));
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }

    /**
      * @description 用户更新 
      * @author liujinshan
      * param null
      * @return 
      * @throws 
      * @date 2020/4/24 15:15
      */
    @PostMapping(value = "update", produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TUser> update(@RequestBody @Validated TUserReqVO tUserReqVO, BindingResult bindingResult){
        BaseResponseResult<TUser> responseResult = new BaseResponseResult<>();

        if (bindingResult.hasErrors()){
            responseResult.setErrCode(ErrorCodesEnum.REQUEST_PARAM_ERROR.code());
            responseResult.setErrMsg(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return responseResult;
        }
        try {
            itUserService.update(tUserReqVO);
            responseResult = BaseResponseResult.ok();
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }


    /**
      * @description 删除用户
      * @author liujinshan
      * param null
      * @return 
      * @throws 
      * @date 2020/4/24 15:15
      */
    @PostMapping(value = "delete",produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TUser> delete(@RequestBody @Validated TUserReqVO tUserReqVO, BindingResult bindingResult){
        BaseResponseResult<TUser> responseResult = new BaseResponseResult<>();
        if (bindingResult.hasErrors()){
            responseResult.setErrCode(ErrorCodesEnum.REQUEST_PARAM_ERROR.code());
            responseResult.setErrMsg(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return responseResult;
        }
        try {
            itUserService.delete(tUserReqVO);
            responseResult = BaseResponseResult.ok();
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }


    /**
      * @description 新增
      * @author liujinshan
      * param tUser
      * @return responseResult
      * @date 2020/4/24 15:17
      */
    @PostMapping( value = "/add" ,produces = {"application/json"}, consumes = {"application/json"})
    public BaseResponseResult<TUser> add(@RequestBody TuerReqInsertVO tuerReqInsertVO){
        BaseResponseResult<TUser> responseResult = new BaseResponseResult<>();
        try {
            itUserService.add(tuerReqInsertVO);
            responseResult = BaseResponseResult.ok();
        } catch (BusinessException e) {
            responseResult.setErrMsg(e.getMsg());
            responseResult.setErrCode(e.getCode());
        } catch (Exception e) {
            responseResult.setErrMsg(e.toString());
            responseResult.setErrCode(ErrorCodesEnum.OTHER.code());
        }
        return responseResult;
    }



}
